/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gs.gms.messanger;

import com.gs.gms.model.dto.UserDto;

/**
 *
 * @author Sabuj
 */
public class MessengerContext {
    private static MessengerContext context;
    
    private MessengerContext(){
        
    }
    
    public static MessengerContext getContext(){
        if(null != context)
            return context;
        synchronized(MessengerContext.class){
            if(null == context)
                context = new MessengerContext();
        }
        return context;
    }
    
    public synchronized static MessengerContext resetContext(){
        context = new MessengerContext();
        return context;
    }
    
    public static final String MQ_TCP_URL = "tcp://localhost:61616";
    public static final String MQ_SSL_URL = "ssl://localhost:61617"; 
        //ssl://0.0.0.0:61617?brokerName=GSI_MQ_Broker
    public static final String MQ_SSL_CONF_PATH = "C:\\TOOLS\\MQ\\ks\\";
    
    
    public static final String CLIENT_A = "A";
    public static final String CLIENT_B = "B";
    
    private BasicAuthenticationClient basicAuthenticationClient;
    private String currentSessionId; 
    private String currentUserName;
    private UserDto currentUser;

    public BasicAuthenticationClient getBasicAuthenticationClient() {
        return basicAuthenticationClient;
    }

    public UserDto getCurrentUser() {
        return currentUser;
    }
    
    public String getCurrentSessionId() {
        return currentSessionId;
    }

    public void setCurrentSessionId(String currentSessionId) {
        this.currentSessionId = currentSessionId;
    }

    public String getCurrentUserName() {
        return currentUserName;
    }

    public void setCurrentUserName(String currentUserName) {
        this.currentUserName = currentUserName;
    }
    
    public boolean doLogin(String userName, String password) throws Exception{
        BasicAuthenticationClient authenticationClient = 
                new BasicAuthenticationClient("localhost", 8080);
        String sessionId = authenticationClient.login(userName, password);
        if(null != sessionId){
            basicAuthenticationClient = authenticationClient;
            currentSessionId = sessionId;
            currentUserName = userName;
            
            
            return true;
        }
        return false;
    }
    
    /*
    HttpClient httpClient = HttpUtility.getLoginHttpClient(u, p);
        try{
            HttpPost httpPost = new HttpPost(SERVER_BASE_ADDRESS);
           
            HttpResponse response = httpClient.execute(httpPost);
            if(null != response && null != response.getStatusLine()
                    && (HttpStatus.SC_MOVED_TEMPORARILY == response.getStatusLine().getStatusCode()
                    || HttpStatus.SC_OK == response.getStatusLine().getStatusCode())){
                Header[] cookieHeaders = response.getHeaders("Set-Cookie");
                if(null != cookieHeaders && cookieHeaders.length > 0){
                    for (Header header : cookieHeaders) {
                        String value = header.getValue();
                        currentSessionId = StringUtils.substringBetween(value, 
                                GmsCommonConstants.SESSION_COOKIE_NAME, ";");
                        break;
                    }
                }
                
                Header[] locationHeaders = response.getHeaders("Location");
                if(null != locationHeaders && locationHeaders.length > 0){
                    for (Header header : locationHeaders) {
                        String value = header.getValue();
                        if(SERVER_BASE_ADDRESS.equals(value))
                            return true;
                    }
                }
            }
        } finally {
            if(null != httpClient)
                httpClient.getConnectionManager().shutdown();
        }
        return false;
    */
}
